package resolve_gene_to_single_transcript;


require Exporter;


our @ISA = qw(Exporter);
our $VERSION = 1.00;
our @EXPORT = qw(get_prot_coding_loci get_sorted_pairswise_ds overlap read_ds_pairs);

use FindBin;
use lib ("$FindBin::Bin/..", "/net/cpp-group/Leo/bin", "$FindBin::Bin");
use strict;
use Carp;
use warnings;
use die_error;




#_________________________________________________________________________________________
#
#	Get ensp coding loci
#
#_________________________________________________________________________________________
# FUNC_2
sub get_prot_coding_loci($$$\%)
{
	my ($dbh, $ortho_name1, $ortho_name2, $map_ensp_to_loci) = @_;
	my $sql_cmd =<<"PL/SQLCMD";
    SELECT
        prot_id,
        start,
        finish
    FROM
        taxon.prot_coding_structures NATURAL join
        ens_id
    WHERE
        species = '$ortho_name1' OR species = '$ortho_name2';
PL/SQLCMD
	my $data = $dbh->selectall_arrayref($sql_cmd)		or die DBI::errstr;
	for (@$data)
	{
		$map_ensp_to_loci->{$_->[0]} = [$_->[1], $_->[2]];
	}
}



#_________________________________________________________________________________________
#
#	get_sorted_pairswise_ds
#
#		get list of dS relationships between supplied lists of transcripts
#
#_________________________________________________________________________________________
# FUNC_7.1
use constant dS  => 0;
sub get_sorted_pairswise_ds(\@\@\%)
{
	my ($all_ensp1, $all_ensp2, $seq_pairs_dS) = @_;
	my @ds;
	for my $ensp1 (@$all_ensp1)
	{
		for my $ensp2 (@$all_ensp2)
		{
			next if $ensp1 eq $ensp2;
			if (exists $seq_pairs_dS->{$ensp1}{$ensp2})
			{
				push(@ds, [$seq_pairs_dS->{$ensp1}{$ensp2}, $ensp1, $ensp2]); 
			}
		}
	}
	return sort {$a->[dS] <=> $b->[dS]} @ds;
}





#_________________________________________________________________________________________
#
#	overlap
#
#		check if loci overlap
#
#_________________________________________________________________________________________
# FUNC_7.211
sub overlap($$\%\%)
{
	my ($ensp1,  $ensp2,  $map_ensp_to_loci, $ensp_sans_loci) = @_;
	if (!exists $map_ensp_to_loci->{$ensp1})
	{
		$ensp_sans_loci->{$ensp1}++;
		return 1;
	}
	if (!exists $map_ensp_to_loci->{$ensp2})
	{
		$ensp_sans_loci->{$ensp1}++;
		return 1;
	}
	my ($b1,  $e1) = @{$map_ensp_to_loci->{$ensp1}};
	my ($b2,  $e2) = @{$map_ensp_to_loci->{$ensp2}};
	#print STDERR "($b1,  $e1, $b2,  $e2)\n";
	#make sure $b1 < $b2
	($b1,  $e1, $b2,  $e2) = ($b2,  $e2, $b1,  $e1) if ($b1 > $b2);
	return ($e1 > $b2) ? 1 : 0;
}



#_________________________________________________________________________________________
#
#	read_ds_pairs
#
#_________________________________________________________________________________________
sub read_ds_pairs($\%$)
{
	my ($file, $seq_pairs_dS, $ids_to_keep) = @_;
	open_or_die *FH_DS, $file;
	while (<FH_DS>)
	{
		chomp;
		my ($ensp1, $ensp2, $ds) = split(/\t/, $_, 4);
		if (defined $ids_to_keep)
		{
			if (exists $ids_to_keep->{$ensp1} &&
				exists $ids_to_keep->{$ensp2})
			{
				$seq_pairs_dS->{$ensp2}{$ensp1} = 
				$seq_pairs_dS->{$ensp1}{$ensp2} = $ds;
			}
		}
		else
		{
			$seq_pairs_dS->{$ensp2}{$ensp1} = 
			$seq_pairs_dS->{$ensp1}{$ensp2} = $ds;
		}
	}
}


1;
